package com.tdt.alarm.controller;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.tdt.alarm.R;
import com.tdt.alarm.model.AlarmDBHelper;
import com.tdt.alarm.model.SetAlarm;
import com.tdt.alarm.model.Time;
import com.tdt.alarm.model.seekbar.SeekArc;
import com.tdt.alarm.model.seekbar.SeekArc.OnSeekArcChangeListener;

public class AddAlarm extends Activity {

	private SeekArc mSeekArcMin;
	private SeekArc mSeekArcHour;

	private TextView mSeekArcMinProgress;
	private TextView mSeekArcHourProgress;

	private Button btnAmPm;
	Button btnAdd;
	Button btnCancel;
	EditText edtAlarmLabel;
	TextView tvRepeat;

	AlarmDBHelper alarmDB;

	private boolean isAM;
	
	SetAlarm newAlarm = new SetAlarm();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.holo_sample);

		alarmDB = new AlarmDBHelper(getApplicationContext());
		

		mSeekArcMin = (SeekArc) findViewById(R.id.seekArcMin);
		mSeekArcHour = (SeekArc) findViewById(R.id.seekArcHour);

		mSeekArcMinProgress = (TextView) findViewById(R.id.seekArcMinProgress);
		mSeekArcHourProgress = (TextView) findViewById(R.id.seekArcHourProgress);

		btnAmPm = (Button) findViewById(R.id.btnAmPm);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		edtAlarmLabel = (EditText) findViewById(R.id.edtLabelAlarm);
		tvRepeat = (TextView) findViewById(R.id.tvAddRepeat);

		isAM = true;
		mSeekArcMin.setmMax(60);
		mSeekArcMin.setArcRotation(0);
		mSeekArcMin.setStartAngle(0);
		mSeekArcMin.setSweepAngle(360);
		mSeekArcMin.setArcWidth(20);
		mSeekArcMin.setProgressWidth(5);
		mSeekArcMin.setRoundedEdges(true);
		mSeekArcMin.setTouchInSide(true);
		mSeekArcMin.setClockwise(true);

		mSeekArcHour.setmMax(12);
		mSeekArcHour.setArcRotation(0);
		mSeekArcHour.setStartAngle(0);
		mSeekArcHour.setSweepAngle(360);
		mSeekArcHour.setArcWidth(20);
		mSeekArcHour.setProgressWidth(5);
		mSeekArcHour.setRoundedEdges(true);
		mSeekArcHour.setTouchInSide(true);
		mSeekArcHour.setClockwise(true);

		mSeekArcMin.setOnSeekArcChangeListener(new OnSeekArcChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				mSeekArcMinProgress.setText(String.valueOf(progress % 60));
			}
		});

		mSeekArcHour.setOnSeekArcChangeListener(new OnSeekArcChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
			}

			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				progress %= 12;
				// if (isAM) {
				// progress += 12;
				// progress %= 24;
				// }
				mSeekArcHourProgress.setText(String.valueOf(progress));
			}
		});

		btnAmPm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v.getId() == btnAmPm.getId()) {
					if (isAM) {
						isAM = false;
						btnAmPm.setBackgroundResource(R.drawable.custom_button_moon);
					} else {
						isAM = true;
						btnAmPm.setBackgroundResource(R.drawable.custom_button_sunny);
					}
				}
			}
		});
		
		

		btnAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int hour;
				if (!isAM) {
					hour = Integer.parseInt((String) mSeekArcHourProgress
							.getText()) + 12;
				} else {
					hour = Integer.parseInt((String) mSeekArcHourProgress
							.getText());
				}

				int min = Integer.parseInt((String) mSeekArcMinProgress
						.getText());
				Time time = new Time(hour, min, 0);
				
				RingtoneManager mRingtoneManager = new RingtoneManager(getApplicationContext());
				mRingtoneManager.setType(RingtoneManager.TYPE_RINGTONE);

			    int length = mRingtoneManager.getCursor().getCount();
				Uri uri = mRingtoneManager.getRingtoneUri(0);
				
				newAlarm.setRingtone(uri);
				newAlarm.setTime(time);
				newAlarm.setCheckActive(true);
				String label = "Alarm name";
				try {
					label = edtAlarmLabel.getText().toString();
				} catch (Exception ex) {
					label = "Alarm name";
				}
				newAlarm.setLabel(label);

				// Utils.writeAlarmToFile(alarm);
				// change to DB
				alarmDB.createAlarm(newAlarm);

				Intent i = new Intent(AddAlarm.this, MainActivity.class);
				startActivity(i);
				finish();
			}
		});

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ArrayList<SetAlarm> list = Utils.readFile();
				// Toast.makeText(getApplicationContext(),"cancel"+list.size(),
				// Toast.LENGTH_LONG).show();
				Intent i = new Intent(AddAlarm.this, MainActivity.class);
				startActivity(i);
				finish();
			}
		});
		tvRepeat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickRepeate();
				
			}
		});

		
	}
	public void onClickRepeate() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Repeate");
		// builder.setMessage("Are you sure you want to delete this entry?");
		LayoutInflater layoutInflater = LayoutInflater.from(AddAlarm.this);
		View promptView = layoutInflater.inflate(R.layout.set_alarm_repeat, null);
		builder.setView(promptView);
//		final CheckBox cb2 = (CheckBox) promptView.findViewById(R.id.CheckBox2);
//		final CheckBox cb3 = (CheckBox) promptView.findViewById(R.id.CheckBox3);
//		final CheckBox cb4 = (CheckBox) promptView.findViewById(R.id.CheckBox4);
//		final CheckBox cb5 = (CheckBox) promptView.findViewById(R.id.CheckBox5);
//		final CheckBox cb6 = (CheckBox) promptView.findViewById(R.id.CheckBox6);
//		final CheckBox cb7 = (CheckBox) promptView.findViewById(R.id.CheckBox7);
//		final CheckBox cb8 = (CheckBox) promptView.findViewById(R.id.CheckBox8);
		
		
		builder.setPositiveButton(android.R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						
//						newAlarm.setRepeatingDay(1, cb2.isChecked());
//						newAlarm.setRepeatingDay(2, cb3.isChecked());
//						newAlarm.setRepeatingDay(3, cb4.isChecked());
//						newAlarm.setRepeatingDay(4, cb5.isChecked());
//						newAlarm.setRepeatingDay(5, cb6.isChecked());
//						newAlarm.setRepeatingDay(6, cb7.isChecked());
//						newAlarm.setRepeatingDay(0, cb8.isChecked());

					}
				})
				.setNegativeButton(android.R.string.no,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								// do nothing
							}
						})
				// .setIcon(android.R.drawable.ic_dialog_alert)
				.show();
	}

}
